package com.springclouddemo.demo.PayController;


import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class PayIndex {
    private Logger logger = LogManager.getLogger(PayIndex.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;
    /**
     * 消费eureka-client-mul提供的服务，可以选中其它的服务，根据需求变化
     * */
    @RequestMapping("/consume")
    public String consumeService(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client-mul");
        String url = "http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/client/dc";
        String message = restTemplate.getForObject(url,String.class);
        logger.info(message);
        return message;
    }
}
